package com.tecklab.demo.image;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;

/**
 * @Deacription TODO
 * @Author sunking
 * @Date 2022/12/28} 下午11:57
 **/
public class ImageUtil {

    public static void splitImage() throws IOException, InterruptedException {
        InputStream inputStream = new BufferedInputStream(
                new FileInputStream(new File("/Users/sun/Desktop/pic_test/1.jpeg")));

        BufferedImage originImage = ImageIO.read(inputStream);

        int dstWidth = originImage.getWidth();
        int dstHeight = 400;

        BufferedImage bufferedImage = new BufferedImage(dstWidth, dstHeight, originImage.getType());
        for (int i = 0; i < 4; i++) {
            Graphics2D graphics2D = bufferedImage.createGraphics();
            boolean flag = graphics2D.drawImage(originImage,
                    0, 0, dstWidth, dstHeight,
                    0, dstHeight * i, dstWidth, dstHeight * (i + 1), null);
            System.out.println("graphics2D.drawImage:\t" + flag);
            graphics2D.dispose();
            ImageIO.write(bufferedImage, "jpeg", new File("/Users/sun/Desktop/pic_test/split" + i + ".jpeg"));
        }


    }

    public static void main(String[] args) throws IOException, InterruptedException {
        splitImage();
    }
}
